package com.rrd.dw.mr.gzip.kafka;

import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.kafka.CrawlResult;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.app.ApplistRecords;
import com.ucredit.babel.common.model.app.AppList;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;

@Deprecated
public class AppListParse {
    public static final String CRT001 = "\001";
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 应用列表
     *
     * @param obj
     * @throws InterruptedException
     * @throws IOException
     */
    public void parseJob(Context context, ApplistRecords obj, String successful) throws IOException, InterruptedException {

        StringBuilder sb = new StringBuilder(200);
        Job job = obj.getJob();
        CrawlResult result = job.getResult();
        if (result == null) {
            result = new CrawlResult();
        }
        String s = "";
        //主键,身份证+job_id+时间
        String user_id = generateId(obj.getUserKey() == null ? "" : obj.getUserKey(), job.getJobID(), String.valueOf(obj.getTimestamp()));
        sb.append(user_id).append(CRT001);
        //user_key
        sb.append(obj.getUserKey() == null ? "" : obj.getUserKey()).append(CRT001);
        sb.append(obj.getTimestamp()).append(CRT001);
        //job_id
        sb.append(job.getJobID() == null ? "" : job.getJobID()).append(CRT001);
        //是否结束戳
        sb.append(job.isFinished() == null ? "" : job.isFinished()).append(CRT001);
        //是否请求
        sb.append(job.isRequested() == null ? "" : job.isRequested()).append(CRT001);
        //是否成功
        sb.append(successful).append(CRT001);
        //起始时间
        sb.append(job.getStartTime() == null ? "" : job.getStartTime().getTime()).append(CRT001);
        //结束时间
        sb.append(job.getEndTime() == null ? "" : job.getEndTime().getTime()).append(CRT001);
        //数据文件
        sb.append(job.getDataFile() == null ? "" : job.getDataFile()).append(CRT001);
        //系统ID
        sb.append(job.getSystemID() == null ? "" : job.getSystemID()).append(CRT001);
        //查询编号
        sb.append(job.getQuery() == null ? "" : job.getQuery()).append(CRT001);
        //尝试次数
        sb.append(job.getTryTimes() == null ? "0" : job.getTryTimes()).append(CRT001);
        //JOB类型
        sb.append(result.getType() == null ? "" : result.getType()).append(CRT001);
        //返回消息
        sb.append(result.getMessage() == null ? "" : result.getMessage()).append(CRT001);
        //代码
        sb.append(result.getCode()).append(CRT001);
        //时间戳
        sb.append(obj.getTimestamp());
        s = sb.toString().replace("\n", " ");
        key.set("appListJob");
        val.set(s);
        context.write(key, val);
        sb.delete(0, sb.length());
    }

    private static String generateId(String userkey, String jobid, String ts) {
        return MD5Util.getMd5(userkey + jobid + ts);
    }

    /**
     * 通话记录明细
     *
     * @param obj
     * @throws InterruptedException
     * @throws IOException
     */
    public void parseDetail(Context context, ApplistRecords obj) throws IOException, InterruptedException {

        StringBuilder sb = new StringBuilder(200);
        Job job = obj.getJob();
        List<AppList> list = obj.getAppList();
        String user_id = generateId(obj.getUserKey() == null ? "" : obj.getUserKey(), job.getJobID(), String.valueOf(obj.getTimestamp()));
        int i = 0;
        for (AppList c : list) {
            i++;

            sb.append(user_id).append(CRT001);
            sb.append(obj.getUserKey()).append(CRT001);
            //时间戮
            sb.append(obj.getTimestamp()).append(CRT001);
            //序号
            sb.append(i).append(CRT001);
            //通讯录人名
            sb.append(c.getAppName() == null ? "" : c.getAppName()).append(CRT001);
            //通讯录号码
            sb.append(c.getPkg() == null ? "" : c.getPkg()).append(CRT001);
            //公司名称(对应hbase remark)
            sb.append(c.getVersion() == null ? "" : c.getVersion()).append(CRT001);
            //邮件地址
            sb.append(c.getInstallTime() == null ? "" : c.getInstallTime()).append(CRT001);
            //职位
            sb.append(c.getLastTime() == null ? "" : c.getLastTime()).append(CRT001);
            //系统类型
            String s = sb.toString().replace("\n", " ");
            s = MD5Util.getMd5(s) + CRT001 + s;
            key.set("appList");
            val.set(s);
            context.write(key, val);
            sb.delete(0, sb.length());
        }

    }
}
